<template>
	<view class="register">
	
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="手机号"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="20"
					placeholder="密码6-16位，必须包含数字和字母"
					isShowPass
				></wInput>
				<wInput
					v-model="verCode"
					type="number"
					maxlength="6"
					placeholder="验证码"
					isShowCode
					ref="runCode"
					@setCode="getVerCode()"
				></wInput>
					
			</view>
				
			<wButton 
				class="wbutton"
				text="注 册"
				:rotate="isRotate" 
				@click.native="startReg()"
			></wButton>
			
			<!-- 底部信息 -->
			<view class="footer">
				<text 
					@tap="isShowAgree" 
					class="cuIcon"
					:class="showAgree?'cuIcon-radiobox':'cuIcon-round'"
				> 同意</text>
				<!-- 协议地址 -->
				<navigator url="" open-type="navigate">《协议》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import {sendRegisterPhoneSms} from '../../apis/third_service.js' //发送验证码
	import {register}  from '../../apis/auth_apis' //注册
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData:'', // 用户/电话
				passData:'', //密码
				verCode:"", //验证码
				showAgree:false, //协议是否选择
				isRotate: false, //是否加载旋转
			}
		},
		components:{
			wInput,
			wButton,
		},
		methods: {
			isShowAgree(){
				//是否选择协议
				this.showAgree = !this.showAgree;
			},
			getVerCode(){
				//判断是否输入手机号，手机号是否规范
				const reg = /^1[3456789]\d{9}$/;
				if (this.phoneData.length == "" || !reg.test(this.phoneData)) {
					uni.showToast({
						icon: 'none',
						mask: true,
						duration: 1000,
						title: '手机号格式不正确'
					});
					return;
				}
				console.log("获取验证码")
				//获取验证码
				sendRegisterPhoneSms({
					phoneNumber:this.phoneData,
				}).then(res=>{
					this.$refs.runCode.$emit('runCode');
					if(res.code==20010){
						uni.showToast({
							icon: 'none',
							mask: true,
							duration: 1000,
							title: '发送成功'
						});
					}else{
						uni.showToast({
							icon: 'none',
							mask: true,
							duration: 1000,
							title: '发送失败'
						});
					}
				})
			},
		    startReg() {
				//注册
				if (this.showAgree == false) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '请先同意《协议》'
				    });
				    return false;
				}
				//判断是否输入手机号，手机号是否规范
				const reg = /^1[3456789]\d{9}$/;
				if (this.phoneData.length == "" || !reg.test(this.phoneData)) {
					uni.showToast({
						icon: 'none',
						mask: true,
						duration: 1000,
						title: '手机号格式不正确'
					});
					return;
				}
				//判断是否输入密码，密码是否规范，长度为6-16位，包含数字和字母，可以有特殊字符
				const phoneReg = /^(?=.*[0-9])(?=.*[a-zA-Z])(.{6,16})$/;
				if (this.passData.length == "" || this.passData.length < 6 || this.passData.length > 16 || !phoneReg.test(
						this.passData)) {
					uni.showToast({
						icon: 'none',
						mask: true,
						duration: 1000,
						title: '密码格式不正确'
					});
					return;
				}
				if(this.verCode.length!=6){
					uni.showToast({
						icon: 'none',
						mask: true,
						duration: 1000,
						title: '验证码不正确'
					});
					return;
				}
				console.log("注册成功")
				uni.showLoading({
					mask:true,
					title:'创建中...',
				})
				register({
					registerVo:{
						phoneNumber:this.phoneData,
						password:this.passData,
						smsCode:this.verCode,
					}
				}).then(res=>{
					console.log(res)
					if(res.code!=20020){
						uni.hideLoading();
						uni.showToast({
							icon: 'none',
							mask: true,
							duration: 500,
							title: res.msg
						});
						return;
					}else{
						uni.hideLoading();
						uni.showToast({
							icon: 'success',
							mask: true,
							duration: 500,
							title: '注册成功'
						});
						setTimeout(function(){
							uni.reLaunch({
								url:'/pages/login/login'
							})
						},200)
					}
				})
		    }
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>